package com.propic.www;

import com.leapmotion.leap.*;
import javafx.application.Platform;


/**
 *
 * @author johan
 */
public class MyLeapListener extends Listener {
    int flag = 0;

    int cnt = 0;
    long start = 0;
    private final Mainwindows app;

    public MyLeapListener(Mainwindows main) {
        this.app = main;
    }

    @Override
    public void onConnect(Controller controller) {
        controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
        controller.enableGesture(Gesture.Type.TYPE_SWIPE);
        System.out.println("connected");
    }

    @Override
    public void onFrame(Controller controller) {
        Frame frame = controller.frame();
        HandList hands = frame.hands();
        if (!hands.isEmpty()) {
            Hand hand = hands.get(0);
            final float x = hand.palmPosition().getX();
            float y = hand.palmPosition().getY();
            float z = hand.palmPosition().getZ();

//            float xm = hand.fingers()
            FingerList fingers = hand.fingers();




            Finger finger1 = fingers.get(0);
            float mx = finger1.tipPosition().getX();
            float my = finger1.tipPosition().getY();
            float mz = finger1.tipPosition().getZ();

            Finger finger2 = fingers.get(1);
            float sx = finger2.tipPosition().getX();
            float sy = finger2.tipPosition().getY();
            float sz = finger2.tipPosition().getZ();
//
            Finger finger3 = fingers.get(2);
            float zx = finger3.tipPosition().getX();
            float zy = finger3.tipPosition().getY();
            float zz = finger3.tipPosition().getZ();

            Finger finger4 = fingers.get(3);
            float wx = finger4.tipPosition().getX();
            float wy = finger4.tipPosition().getY();
            float wz = finger4.tipPosition().getZ();

            Finger finger5 = fingers.get(4);
            float xx = finger5.tipPosition().getX();
            float xy = finger5.tipPosition().getY();
            float xz = finger5.tipPosition().getZ();





//            System.out.println("x = " + mx + ", y = " + my + ", z = " + mz);

            Platform.runLater(new Runnable() {

                @Override
                public void run() {
//                    app.centermX().set(mx*2);
//                    app.centermY().set(-my*2);
//
//                    app.centersX().set(sx*2);
//                    app.centersY().set(-sy*2);
//
//                    app.centerzX().set(zx*2);
//                    app.centerzY().set(-zy*2);
//
//                    app.centerwX().set(wx*2);
//                    app.centerwY().set(-wy*2);
//
//                    app.centerxX().set(xx*2);
//                    app.centerxY().set(-xy*2);
//                    if (Math.abs(mx*2-sx*2) < 10){
//                        app.closeAll();
//
//                    }
                    GestureList gestures = frame.gestures();
                    if (gestures.count() != 0){
                        System.out.println();

                        for (Gesture in : gestures){
                            System.out.print(in.type()+"   ");
                            if (in.type() == Gesture.Type.TYPE_SWIPE && Mainwindows.count == 0){
                                Mainwindows.count++;
                                app.setShowAll();
                            }
                            if (in.type() == Gesture.Type.TYPE_CIRCLE){
                                if (Mainwindows.count == 1 && Mainwindows.count2 == 0){
                                    Mainwindows.count2++;
                                    app.setShowAllclose();
                                }

                            }

                        }

                    }
                }

            });


        }
//        float f = frame.currentFramesPerSecond();
//        // System.out.println("got frame! "+controller.frame());
//        double tot = 12;
//        String msg = "";
//        long n = System.currentTimeMillis();
//        for (int i = 0; i < 3000; i++) {
//            double d = Math.random() * i;
//            tot = tot + d;
//            String u = UUID.randomUUID().toString();
//            msg = msg + u;
//        }
//        long m = System.currentTimeMillis();
//        System.out.println("TIME = "+(m-n));
//        if (start == 0) {
//            start = System.currentTimeMillis();
//        }
//        cnt++;
//        if (cnt%100==0) {
//            long now = System.currentTimeMillis();
//            double rate = (double)cnt/(now - start);
//            System.out.println("rate = "+rate+" -- "+f);
//        }
//
    }

}